import 'package:flutter/material.dart';

import 'model/LevelModel.dart';

typedef RainbowColor = Color Function();

class RainbowLevelField extends StatefulWidget {
  final double width, height, fill, select;
  final Color background;
  final LevelController controller;
  final RainbowColor color;
  final int max;
  final bool tap;

  RainbowLevelField({Key key,
    this.width,
    this.height,
    this.controller,
    this.background=Colors.grey,
    this.color,
    this.max=1000,
    this.fill=0.95,
    this.select=0,
    this.tap=true}) : super(key: key);

  @override
  State<StatefulWidget> createState() => RainbowLevelFieldState();
}

class LevelPainter extends CustomPainter {

  final double width, height, selectWidth;
  final Color background;
  final RainbowColor color;
  final int max;

  int a, r, g, b;
  int step = 0;

  LevelPainter({
    this.width,
    this.height,
    this.selectWidth,
    this.background,
    this.color,
    this.max}):super();

  Color createColor(){
    Color color;
    if (0 == step){
      color = Color.fromARGB(a, r, g++, b);
      step += g == 255 ? 1 : 0;
    } else if (1 == step){
      color = Color.fromARGB(a, r, g, b--);
      step += b == 0 ? 1 : 0;
    } else if (2 == step){
      color = Color.fromARGB(a, r++, g, b);
      step += r == 255 ? 1 : 0;
    } else if (3 == step){
      color = Color.fromARGB(a, r, g--, b);
      step += g == 0 ? 1 : 0;
    } else {
      color = Color.fromARGB(a, r, g, b);
    }

    return color;
  }

  @override
  void paint(Canvas canvas, Size size) {
    a = 255;
    r = 0;
    g = 0;
    b = 255;

    double x = 0, itemSize = width / max;
    double endWidth = selectWidth > this.width ? this.width : this.selectWidth;
    // TODO: implement paint
    var paint = Paint();
    printView(paint, canvas, this.width, 0, background); //background
    for (int i=0;i<max;i++) {
      if (x <= selectWidth){
        printView(paint, canvas, endWidth, x, null == this.color ? createColor() : this.color());
        x+=itemSize;
      } else {
        break;
      }
    }
  }

  void printView(Paint paint, Canvas canvas, double endWidth, double x, Color color){
    paint.isAntiAlias = true;
    paint.style = PaintingStyle.fill; //填充
    paint.color = color;
    canvas.drawRect(Rect.fromPoints(Offset(x, height), Offset(endWidth, 0)), paint);
  }



  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }

}

class RainbowLevelFieldState extends State<RainbowLevelField> {
  double x, y;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    x = widget.select > 0 ? widget.select : widget.width;
    y = widget.height;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return GestureDetector(
        child: CustomPaint(
          child: Container(
            width: widget.width,
            height: widget.height,
          ),
          painter: LevelPainter(
            width: widget.width,
            height: widget.height,
            selectWidth: x,
            background: widget.background,
            max: widget.max,
            color: widget.color
          ),
        ),
        onPanDown: (item){
          if (widget.tap){
            setState(() {
              x = item.localPosition.dx;
              y = item.localPosition.dy;
              x = x > widget.width * widget.fill ? widget.width : x;
              if (null != widget.controller){
                widget.controller.value.index = x;
              }
            });
          }
        },
      );
  }

}